package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constrant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.service.CheckItemService;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 菜单管理
 *
 * @author LQQ
 * @date 2019-12-30 18:47
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference//查找服务
    private MenuService menuService;

    //根据id删除菜单

    @RequestMapping("/delete")
    public Result delete(Integer id) {

        try {
            menuService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"服务器正忙");
        }
        return new Result(true, "删除成功");

    }

//分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = menuService.pageQuery(queryPageBean);
        return pageResult;
    }


    //新增菜单
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        try {
            menuService.add(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"增加菜单失败");
        }
        return new Result(true,"新增菜单成功");
    }


    //根据id查询菜单信息,用于回显
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,"查询菜单成功", menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"服务器正忙");
        }
    }


    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        try {
           menuService.update(menu);
            return new Result(true,"编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败");
        }

    }



    /**
     * 查询所有的菜单
     * @return
     */

    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Menu> list = menuService.findAll();
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false,"服务器正忙");
        }

    }
}
